﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace giaoxuhaison
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static int UsersConnected { get; set; }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
            //Application["OnlineUsers"] = 0;
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            /*
             * @author  :   Dungnv
             * @date    :   08/01/2013
             */
            int count_visit = 0;
            //Kiểm tra file count_visit.txt nếu không tồn  tại thì
            if (System.IO.File.Exists(Server.MapPath("~/count_visit.txt")) == false)
            {
                count_visit = 1;
            }
            // Ngược lại thì
            else
            {
                // Đọc dử liều từ file count_visit.txt
                System.IO.StreamReader read = new System.IO.StreamReader(Server.MapPath("~/count_visit.txt"));
                count_visit = int.Parse(read.ReadLine());
                read.Close();
                // Tăng biến count_visit thêm 1
                count_visit++;
            }

            Application.Lock();

            if (Application["OnlineUsers"] == null)
            {
                Application["OnlineUsers"] = 0;
            }
            /*
            if (Application["Access"] == null)
            {
                Application["Access"] = 0;
            }            
            */
            //Application["Access"] = (int)Application["Access"] + 1;
            Application["Access"] = count_visit;

            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            UsersConnected++;
            Application.UnLock();

            // Lưu dử liệu vào file  count_visit.txt
            System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/count_visit.txt"));
            writer.WriteLine(count_visit);
            writer.Close();

        }

        protected void Session_End(object sender, EventArgs e)
        {
            /*
             * @author  :   Dungnv
             * @date    :   08/01/2013
             */

            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            UsersConnected--;
            Application.UnLock();            
        }

        protected void Application_End()
        {
            /*
            * @author  :   Dungnv
            * @date    :   08/01/2013
            */

            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
        }
    }
}